#-*- coding:utf-8 -*-
# encoding:utf-8
import base64
import urllib
import urllib2
import shutil,os
import string
import json,redis
import pymysql
'''
人脸搜索
'''
r = redis.Redis(host='127.0.0.1', port=6379,db=0)
db = pymysql.connect(user='root', password='root', database='face')

sign=10;
while(1):
	#path='img/'
	#facelist = os.listdir(path)
	#print(r.llen("facebase64"))
	if not r.llen("facebase64"):
		#print "目录为空，等待图片"
		continue;


	request_url = "https://aip.baidubce.com/rest/2.0/face/v3/search"
	filename1=r.rpop("facebase64")
	filename = filename1.split('.jpg')[0]
	img = filename1.split('.jpg')[1]

	pa = json.dumps({"image": img, "image_type": "BASE64","group_id_list":"grouptest","quality_control":"LOW","liveness_control":"NORMAL"})
	params = str(pa)

	access_token = '24.7c3398e3bac533fe969176729fcbef1c.2592000.1529656709.282335-11281836'
	request_url = request_url + "?access_token=" + access_token
	request = urllib2.Request(url=request_url, data=params)
	request.add_header('Content-Type', 'application/json')
	response = urllib2.urlopen(request)
	content = response.read()
	if content:
                j = json.loads(content)
        if(j['error_code']!=0):
            continue
	print j['result']['user_list'][0]['user_info']
	print j['result']['user_list'][0]['score']
	if(j['result']['user_list'][0]['score']>=80):
		cursor = db.cursor()
		num3=1;
		num2=str(j['result']['user_list'][0]['user_info'])
		num2=num2.split('.JPG')[0]
		num2=num2.split('/')[1]
		num=sign
		sign=sign+1
		data=(num,num2,num3,"facebase",1,filename)
		cursor.execute('insert  into register(id,student_id,teacher_id,coure_name,situation,time) values'+str(data))
		db.commit()
		cursor.close()
		print str(data)
